﻿
using CommonBase;
using CustomControls.PoptipCtr.Base;
using CustomControls.PoptipCtr;
using LogProjectManager;
using Prism.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Reflection;

namespace MyAppProjectControl.BaseViewModels
{
    public class BaseViewModel : ViewBindModelBase
    {
        private bool IsHaveShow { set; get; }

        public List<int> Limits { get; set; } = new List<int>()
        {
            10,15,20
        };

        private int _SumPageCount = 0;

        public int SumPageCount
        {
            get
            {
                return _SumPageCount;
            }
            set
            {
                SetProperty(ref _SumPageCount, value);
            }
        }

        private int _PageCount = 1;

        public int PageCount
        {
            get
            {
                return _PageCount;
            }
            set
            {
                SetProperty(ref _PageCount, value);
            }
        }

        private int _PageItemsCount;

        public int PageItemsCount
        {
            get
            {
                return _PageItemsCount;
            }
            set
            {
                SetProperty(ref _PageItemsCount, value);
            }
        }

        public BaseViewModel(IContainerExtension container) : base(container)
        {
        }

        protected void InitInfo()
        {
            PageItemsCount = Limits.FirstOrDefault();
        }


        protected void ShowDialog(bool _isAutoClose = true, bool _IsShowGray = true, string ContentMsg = "正在为您加载...")
        {
            try
            {
                if (IsHaveShow)
                {
                    return;
                }
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    LayDialogParameter dialogParameter = new LayDialogParameter();
                    dialogParameter.Add("IsAutoClose", _isAutoClose);
                    dialogParameter.Add("IsShowGray", _IsShowGray);
                    dialogParameter.Add("ContentMsg", ContentMsg);
                    dialogParameter.Add("AutoCloseTime", 2000);
                    LayDialogControl.Show(SystemResource.Nav_DialogLodingView, dialogParameter, rest =>
                    {

                    }, SystemResource.Nav_RootDialogTooken);
                    IsHaveShow = true;
                }));

            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Exception, "ShowDialog", ex.ToString());
            }
        }

        protected void CloseDialog(string token = "")
        {
            try
            {
                if (string.IsNullOrEmpty(token))
                {
                    LayDialogControl.CloseAll();
                    IsHaveShow = false;
                }
                else
                {
                    LayDialogControl.Close(token);
                }

            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Exception, "ShowDialog", ex.ToString());
            }
        }

        public static T DeepCopy<T>(T obj)
        {

            if (obj is string || obj.GetType().IsValueType)
            {
                return obj;
            }

            object retval = Activator.CreateInstance(obj.GetType());
            FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
            foreach (FieldInfo field in fields)
            {
                try
                {
                    if (field.GetValue(obj) == null)
                    {
                    }
                    field.SetValue(retval, DeepCopy(field.GetValue(obj)));
                }
                catch (Exception ex)
                {

                }
            }
            return (T)retval;
        }


    }
}
